package com.halixun.d2app.net;

import android.content.Context;
import android.text.TextUtils;

import com.halixun.d2app.app.MyApplication;
import com.halixun.d2app.bean.ClientUserInfo;
import com.halixun.d2app.utils.DataHandle;

public class DataState {
    private static DataState mState;

    public synchronized static DataState getInstance() {
        if (mState == null) {
            mState = new DataState();
        }
        return mState;
    }

    private ClientUserInfo clientUserInfo;

    private boolean loginState = false;

    private boolean isGetDataFromNet = true;

    private String nativeCommodityJson;

    public boolean isResetDownLoadHomeInfo = false;

    public String getNativeCommodityJson() {
        if (TextUtils.isEmpty(nativeCommodityJson)){
            nativeCommodityJson = DataHandle.getNativeInfo(MyApplication.getMyContext());
        }
        if (TextUtils.isEmpty(nativeCommodityJson)){
            nativeCommodityJson = "[]";
        }
        return nativeCommodityJson;
    }

    public void setNativeCommodityJson(String nativeCommodityJson) {
        if (TextUtils.isEmpty(nativeCommodityJson)){
            return;
        }
        this.nativeCommodityJson = nativeCommodityJson;
        DataHandle.setNativeInfo(MyApplication.getMyContext(),nativeCommodityJson);
    }

    public boolean isGetDataFromNet() {
        return isGetDataFromNet;
    }

    public void setGetDataFromNet(boolean getDataFromNet) {
        isGetDataFromNet = getDataFromNet;
        DataHandle.setDataFromType(MyApplication.getMyContext(),getDataFromNet);
    }

    public boolean isLoginState() {
        return loginState;
    }

    public void setClientUserInfo(ClientUserInfo clientUserInfo) {
        this.clientUserInfo = clientUserInfo;
    }

    public ClientUserInfo getClientUserInfo() {
        if (clientUserInfo == null) {
            clientUserInfo = new ClientUserInfo();
        }
        return clientUserInfo;
    }

    /**
     * 设置登录状态
     *
     * @param state
     */
    public void setLoginState(boolean state) {
        this.loginState = state;
    }

    /**
     * 进入app时数据初始化
     *
     * @param context
     */
    public void appEnter(Context context) {
        DataHandle.appEnter(context);
    }

    /**
     * app登陆时数据保存，账号密码登陆和验证码登陆
     *
     * @param info
     */
    public void appLogin(ClientUserInfo info) {
        this.loginState = true;
        this.clientUserInfo = info;
        DataHandle.appLogin(MyApplication.getMyContext(), info);
    }

    /**
     * app推出，下次登陆，app处于登陆状态
     */
    public void appExit() {
        this.loginState = false;
        this.clientUserInfo = null;
    }

    /**
     * app点击退出登录按钮退出，下次进入为未登录状态
     */
    public void appExitLogin() {
        appExit();
        DataHandle.appExit(MyApplication.getMyContext());
    }
}
